package veilingsite.database.nl;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletContext;


public class DBmanager {
	
	private static DBmanager uniqueInstance=null;
	
	private Connection con = null ;

        

	
	private DBmanager(String dbName, String dbURL, String dbLocation){
		if(!dbExists(dbName, dbURL ,dbLocation)) {
			// database bestaat niet
		}
	
	}


	public static synchronized DBmanager getInstance(String dbName, String dbURL, String dbLocation) {
		if (uniqueInstance==null) {
			uniqueInstance = new DBmanager(dbName, dbURL, dbLocation);
		}
		return uniqueInstance;
	}
	
	private boolean dbExists(String dbName, String dbURL, String dbLocation) {
		boolean exists = false ;
		try{
			Class.forName(dbURL) ;
            con = DriverManager.getConnection(dbName+dbLocation);
			exists = true ;
		} catch(Exception e){
			// Do nothing, as DB does not (yet) exist
		}
		return(exists) ;
	}

	public void close(String dbName, String dbURL ) {
		try {
			con = DriverManager.getConnection(dbName + dbURL + ";shutdown=true") ;
		}catch(SQLException se) {
			; // Do Nothing. System has shut down.
		con = null ;
		}
	}
	
	public Connection getConnection() {
		return con;
	}
}
